<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('iaDiagnosisFieldSearch')) {

    function iaDiagnosisFieldSearch($searchData = array())
    {
        $CI = & get_instance();
        $_field_search_attribute = array(
            'textsearch' => array('name' => 'textsearch', 'id' => 'frm-textsearch',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_keyword'),
                'value' => set_value('textsearch', isset($searchData['textsearch']) ? $searchData['textsearch'] : '')
            ),
            'submit' => array(
                'name' => 'btn-Search',
                'class' => 'btn btn-primary',
                'data-loading-text' => $CI->lang->line('Loading'),
                'id' => 'form-btnSearch',
                'value' => $CI->lang->line('Search')
            ),
            'reset' => array(
                'name' => 'btn-Reset',
                'class' => 'btn btn-primary',
                'data-loading-text' => $CI->lang->line('Loading'),
                'id' => 'form-btnReset',
                'value' => $CI->lang->line('Reset')
            ),
        );

        return $_field_search_attribute;
    }

}

if (!function_exists('iaDiagnosisFieldFilter')) {

    function iaDiagnosisFieldFilter($filterData = array(), $filterSelected = 0)
    {
        $CI = & get_instance();

        $_field_filter_attribute = array(
            'dropdown_filter' => array(
                'name' => 'ia_id',
                'data' => $filterData,
                'selected' => $filterSelected,
                'extra' => "id='frm-ia-attribute-filter' class='span3'"
            ),
            'submit' => array(
                'name' => 'btn-Filter',
                'class' => 'btn btn-primary',
                'data-loading-text' => $CI->lang->line('Loading'),
                'id' => 'form-btnSearch',
                'value' => $CI->lang->line('Filter')
            ),
            'reset' => array(
                'name' => 'btn-Reset',
                'class' => 'btn btn-primary',
                'data-loading-text' => $CI->lang->line('Loading'),
                'id' => 'form-btnReset',
                'value' => $CI->lang->line('Reset')
            ),
        );

        return $_field_filter_attribute;
    }

}


//define field to generate form
if (!function_exists('iaDiagnosisFieldForm')) {

    function iaDiagnosisFieldForm($dataDetail = array(), $changeDetail = array(), $types = array())
    {

        $CI = & get_instance();
        $config = $CI->config->config;
        if (count($dataDetail) && !empty($dataDetail)) {
            $active = isset($dataDetail['active']) ? $dataDetail['active'] : 0;
        }
        $options = array();
        if (count($types) && !empty($types)) {

            foreach ($types as $k => $val) {
                if (!isset($options [$val->type_id]))
                    $options [$val->type_id] = array();
                $options [$val->type_id] [$val->id] = $val->name;
            }
        }
        $plancoachRequisOption = array();
        if (isset($options[$config['mapAttribute']['plancoach_requis']])) {
            $plancoachRequisOption = $options[$config['mapAttribute']['plancoach_requis']];
        }
        $option_besoin_affaire = array();
        if (isset($options[$config['mapAttribute']['besoin_affaire']])) {
            $option_besoin_affaire = $options[$config['mapAttribute']['besoin_affaire']];
        }
        $option_besoin_besoin = array();
        if (isset($options[$config['mapAttribute']['besoin_besoin']])) {
            $option_besoin_besoin = $options[$config['mapAttribute']['besoin_besoin']];
        }

        $option_bilancoach_acquis = array();
        if (isset($options[$config['mapAttribute']['bilancoach_acquis']])) {
            $option_bilancoach_acquis = $options[$config['mapAttribute']['bilancoach_acquis']];
        }

        $option_result = array();
        if (isset($options[$config['mapAttribute']['accompagnement_resultat']])) {
            $option_result = $options[$config['mapAttribute']['accompagnement_resultat']];
        }

        $besoin_affaire = isset($dataDetail['besoin_affaire']) ? array($dataDetail['besoin_affaire']) : array();
        $besoin_solution = isset($dataDetail['besoin_solution']) ? $dataDetail['besoin_solution'] : '';
        $besoin_besoin = isset($dataDetail['besoin_besoin']) ? $dataDetail['besoin_besoin'] : array();
        if(count($besoin_besoin)){
            $besoin_besoin = explode('|', $besoin_besoin);
        }

        $besoin_comment = isset($dataDetail['besoin_comment']) ? $dataDetail['besoin_comment'] : '';

        $plancoach_besoin = isset($dataDetail['plancoach_besoin']) ? $dataDetail['plancoach_besoin'] : '';
        $plancoach_besoin = explode('|', $plancoach_besoin);

        $plancoach_theme = isset($dataDetail['plancoach_theme']) ? $dataDetail['plancoach_theme'] : '';
        $plancoach_theme = explode('|', $plancoach_theme);

        $plancoach_theme_sub = isset($dataDetail['plancoach_theme_sub']) ? $dataDetail['plancoach_theme_sub'] : '';


        $plancoach_requis = isset($dataDetail['plancoach_requis']) ? array($dataDetail['plancoach_requis']) : array();
        $plancoach_type = isset($dataDetail['plancoach_type']) ? $dataDetail['plancoach_type'] : '';
        $plancoach_type = explode('|', $plancoach_type);

        $plancoach_type_sub = isset($dataDetail['plancoach_type_sub']) ? $dataDetail['plancoach_type_sub'] : '';

        $actionVendor = isset($dataDetail['plancoach_action_vendor']) ? $dataDetail['plancoach_action_vendor'] : '';
        $plancoach_action_vendor = $actionVendor;
        $plancoach_action_ia = isset($dataDetail['plancoach_action_ia']) ? $dataDetail['plancoach_action_ia'] : '';

        // sub tab 3-1
        $support_provider1 = isset($dataDetail['sub_tab3'][1]['support_provider']) ? $dataDetail['sub_tab3'][1]['support_provider'] : '';
        $support_object1 = isset($dataDetail['sub_tab3'][1]['support_object']) ? $dataDetail['sub_tab3'][1]['support_object'] : '';
        $support_date1 = isset($dataDetail['sub_tab3'][1]['support_date']) ? date('d/m/Y', strtotime($dataDetail['sub_tab3'][1]['support_date'])) : '';
        $support_result1 = isset($dataDetail['sub_tab3'][1]['support_result']) ? array($dataDetail['sub_tab3'][1]['support_result']) : array('');
        $support_comment1 = isset($dataDetail['sub_tab3'][1]['support_comment']) ? $dataDetail['sub_tab3'][1]['support_comment'] : '';
        
        // sub tab 3-2
        $support_provider2 = isset($dataDetail['sub_tab3'][2]['support_provider']) ? $dataDetail['sub_tab3'][2]['support_provider'] : '';
        $support_object2 = isset($dataDetail['sub_tab3'][2]['support_object']) ? $dataDetail['sub_tab3'][2]['support_object'] : '';
        $support_date2 = isset($dataDetail['sub_tab3'][2]['support_date']) ? date('d/m/Y', strtotime($dataDetail['sub_tab3'][2]['support_date'])) : '';
        $support_result2 = isset($dataDetail['sub_tab3'][2]['support_result']) ? array($dataDetail['sub_tab3'][2]['support_result']) : array('');
        $support_comment2 = isset($dataDetail['sub_tab3'][2]['support_comment']) ? $dataDetail['sub_tab3'][2]['support_comment'] : '';
        
        // sub tab 3-3
        $support_provider3 = isset($dataDetail['sub_tab3'][3]['support_provider']) ? $dataDetail['sub_tab3'][3]['support_provider'] : '';
        $support_object3 = isset($dataDetail['sub_tab3'][3]['support_object']) ? $dataDetail['sub_tab3'][3]['support_object'] : '';
        $support_date3 = isset($dataDetail['sub_tab3'][3]['support_date']) ? date('d/m/Y', strtotime($dataDetail['sub_tab3'][3]['support_date'])) : '';
        $support_result3 = isset($dataDetail['sub_tab3'][3]['support_result']) ? array($dataDetail['sub_tab3'][3]['support_result']) : array('');
        $support_comment3 = isset($dataDetail['sub_tab3'][3]['support_comment']) ? $dataDetail['sub_tab3'][3]['support_comment'] : '';
        
        // sub tab 3-4
        $support_provider4 = isset($dataDetail['sub_tab3'][4]['support_provider']) ? $dataDetail['sub_tab3'][4]['support_provider'] : '';
        $support_object4 = isset($dataDetail['sub_tab3'][4]['support_object']) ? $dataDetail['sub_tab3'][4]['support_object'] : '';
        $support_date4 = isset($dataDetail['sub_tab3'][4]['support_date']) ? date('d/m/Y', strtotime($dataDetail['sub_tab3'][4]['support_date'])) : '';
        $support_result4 = isset($dataDetail['sub_tab3'][4]['support_result']) ? array($dataDetail['sub_tab3'][4]['support_result']) : array('');
        $support_comment4 = isset($dataDetail['sub_tab3'][4]['support_comment']) ? $dataDetail['sub_tab3'][4]['support_comment'] : '';
        
        //$support_result = isset($dataDetail['besoin_besoin']) ? array($dataDetail['besoin_besoin']) : array();
        $besoin_solution_sub = isset($dataDetail['besoin_solution_sub']) ? $dataDetail['besoin_solution_sub'] : '';
        $plancoach_requis_sub = isset($dataDetail['plancoach_requis_sub']) ? $dataDetail['plancoach_requis_sub'] : '';
        $bilancoach_acquis = array();
        if (isset($dataDetail['bilancoach_acquis'])) {
            $bilancoach_acquis = array($dataDetail['bilancoach_acquis']);
        }
        $bilancoach_date = '';
        if (isset($dataDetail['bilancoach_date'])) {
            $bilancoach_date = date('d/m/Y', strtotime($dataDetail['bilancoach_date']));
        }
        $bilancoach_comment = isset($dataDetail['bilancoach_comment']) ? $dataDetail['bilancoach_comment'] : '';


        $arrBesoin = array();
        if(count($option_besoin_besoin)) {
            foreach($option_besoin_besoin as $besoin_id => $besoin_name) {
                $arrBesoin[] = array(
                    'name' => 'besoin_besoin[]',
                    'id' => 'besoin_besoin_' . $besoin_id,
                    'class' => 'span3',
                    'checked' => in_array($besoin_id, $besoin_besoin) ? TRUE : FALSE,
                    'value' => set_value('besoin_besoin', $besoin_id),
                    'label' => $besoin_name
                );
            }
        }
//echo '<pre>';
//print_r($arrBesoin);exit;

        //var_dump($besoin_solution); die;
        $_field_form = array(
            // Tab 1 - Besoin
            'besoin_affaire' => array(
                'name' => 'besoin_affaire',
                'id' => 'besoin_affaire',
                'extra' => 'id="besoin_affaire"',
                'options' => $option_besoin_affaire,
                'selected' => $besoin_affaire
            ),
            'besoin_solution' => array(
                'autonomie' => array('name' => 'besoin_solution[]', 'id' => 'besoin_solution_autonomie',
                    'class' => 'span3',
                    'checked' => ($besoin_solution == 1) ? TRUE : FALSE,
                    'value' => set_value('name', 1)
                ),
                'accompagne' => array('name' => 'besoin_solution[]', 'id' => 'besoin_solution_accompagne',
                    'class' => 'span3',
                    'checked' => ($besoin_solution == 2) ? TRUE : FALSE,
                    'value' => set_value('name', 2)
                ),
            ),
            'besoin_solution_sub' => array('name' => 'besoin_solution_sub', 'id' => 'besoin_solution_sub',
                'class' => 'span3',
                'value' => set_value('name', $besoin_solution_sub)
            ),
            'plancoach_requis_sub' => array('name' => 'plancoach_requis_sub', 'id' => 'plancoach_requis_sub',
                'class' => 'span3',
                'value' => set_value('name', $plancoach_requis_sub)
            ),


            'plancoach_theme_sub' => array('name' => 'plancoach_theme_sub', 'id' => 'plancoach_theme_sub',
                'class' => 'span3',
                'value' => set_value('name', $plancoach_theme_sub)
            ),

            'plancoach_type_sub' => array('name' => 'plancoach_type_sub', 'id' => 'plancoach_type_sub',
                'class' => 'span3',
                'value' => set_value('name', $plancoach_type_sub)
            ),

//            'besoin_besoin' => array(
//                'name' => 'besoin_besoin',
//                'id' => 'besoin_besoin',
//                'extra' => 'id="besoin_besoin"',
//                'options' => $option_besoin_besoin,
//                'selected' => $besoin_besoin
//            ),

            'besoin_besoin' => $arrBesoin,

            'besoin_comment' => array('name' => 'besoin_comment', 'id' => 'besoin_comment',
                'class' => 'span3',
                'value' => set_value('name', $besoin_comment)
            ),
            // Tab 2 - Plan Coaching
            'plancoach_besoin' => array(
                'offre' => array('name' => 'plancoach_besoin[]', 'id' => 'plancoach_besoin_offre',
                    'class' => 'span3',
                    'checked' => in_array('1', $plancoach_besoin) ? TRUE : FALSE,
                    'value' => set_value('name', 1)
                ),
                'outils' => array('name' => 'plancoach_besoin[]', 'id' => 'plancoach_besoin_outils',
                    'class' => 'span3',
                    'checked' => in_array('2', $plancoach_besoin) ? TRUE : FALSE,
                    'value' => set_value('name', 2)
                ),
                'process' => array('name' => 'plancoach_besoin[]', 'id' => 'plancoach_besoin_process',
                    'class' => 'span3',
                    'checked' => in_array('3', $plancoach_besoin) ? TRUE : FALSE,
                    'value' => set_value('name', 3)
                ),
            ),
            'plancoach_theme' => array(
                'detection' => array('name' => 'plancoach_theme[]', 'id' => 'plancoach_theme_detection',
                    'class' => 'span3',
                    'checked' => in_array('1', $plancoach_theme) ? TRUE : FALSE,
                    'value' => set_value('name', 1)
                ),
                'qualification' => array('name' => 'plancoach_theme[]', 'id' => 'plancoach_theme_qualification',
                    'class' => 'span3',
                    'checked' => in_array('2', $plancoach_theme) ? TRUE : FALSE,
                    'value' => set_value('name', 2)
                ),
                'argumentation' => array('name' => 'plancoach_theme[]', 'id' => 'plancoach_theme_argumentation',
                    'class' => 'span3',
                    'checked' => in_array('3', $plancoach_theme) ? TRUE : FALSE,
                    'value' => set_value('name', 3)
                ),
                'elargissment' => array('name' => 'plancoach_theme[]', 'id' => 'plancoach_theme_elargissment',
                    'class' => 'span3',
                    'checked' => in_array('4', $plancoach_theme) ? TRUE : FALSE,
                    'value' => set_value('name', 4)
                ),
                'autre' => array('name' => 'plancoach_theme[]', 'id' => 'plancoach_theme_autre',
                    'class' => 'span3',
                    'checked' => in_array('5', $plancoach_theme) ? TRUE : FALSE,
                    'value' => set_value('name', 5)
                ),
            ),
            'plancoach_requis' => array(
                'name' => 'plancoach_requis',
                'id' => 'plancoach_requis',
                'extra' => 'id="plancoach_requis"',
                'options' => $plancoachRequisOption,
                'selected' => $plancoach_requis
            ),
            'plancoach_type' => array(
                'training' => array('name' => 'plancoach_type[]', 'id' => 'plancoach_type_training',
                    'class' => 'span3',
                    'checked' => in_array('1', $plancoach_type) ? TRUE : FALSE,
                    'value' => set_value('name', 1)
                ),
                'atelier' => array('name' => 'plancoach_type[]', 'id' => 'plancoach_type_atelier',
                    'class' => 'span3',
                    'checked' => in_array('2', $plancoach_type) ? TRUE : FALSE,
                    'value' => set_value('name', 2)
                ),
                'demo' => array('name' => 'plancoach_type[]', 'id' => 'plancoach_type_demo',
                    'class' => 'span3',
                    'checked' => in_array('3', $plancoach_type) ? TRUE : FALSE,
                    'value' => set_value('name', 3)
                ),
                'support' => array('name' => 'plancoach_type[]', 'id' => 'plancoach_type_support',
                    'class' => 'span3',
                    'checked' => in_array('4', $plancoach_type) ? TRUE : FALSE,
                    'value' => set_value('name', 4)
                ),
                'soutien' => array('name' => 'plancoach_type[]', 'id' => 'plancoach_type_soutien',
                    'class' => 'span3',
                    'checked' => in_array('5', $plancoach_type) ? TRUE : FALSE,
                    'value' => set_value('name', 5)
                ),
                'autre' => array('name' => 'plancoach_type[]', 'id' => 'plancoach_type_autre',
                    'class' => 'span3',
                    'checked' => in_array('6', $plancoach_type) ? TRUE : FALSE,
                    'value' => set_value('name', 6)
                ),
            ),
            'plancoach_action_vendor' => array('name' => 'plancoach_action_vendor', 'id' => 'plancoach_action_vendor',
                'class' => 'span3',
                'value' => set_value('name', $plancoach_action_vendor)
            ),
            'plancoach_action_ia' => array('name' => 'plancoach_action_ia', 'id' => 'plancoach_action_ia',
                'class' => 'span3',
                'value' => set_value('name', $plancoach_action_ia)
            ),
            // Tab 3-1
            'support_provider1' => array('name' => 'support_provider1', 'id' => 'support_provider1',
                'class' => 'span3',
                'value' => set_value('name', $support_provider1)
            ),
            'support_object1' => array('name' => 'support_object1', 'id' => 'support_object1',
                'class' => 'span3',
                'value' => set_value('name', $support_object1)
            ),
            'support_date1' => array('name' => 'support_date1', 'id' => 'support_date1',
                'class' => 'span3',
                'value' => set_value('name', $support_date1)
            ),
            'support_result1' => array(
                'name' => 'support_result1',
                'id' => 'support_result1',
                'extra' => 'id="support_result1"',
                'options' => $option_result,
                'selected' => $support_result1
            ),
            'support_comment1' => array('name' => 'support_comment1', 'id' => 'support_comment1',
                'class' => 'span3',
                'value' => set_value('name', $support_comment1)
            ),
            // Tab 3-2
            'support_provider2' => array('name' => 'support_provider2', 'id' => 'support_provider2',
                'class' => 'span3',
                'value' => set_value('name', $support_provider2)
            ),
            'support_object2' => array('name' => 'support_object2', 'id' => 'support_object2',
                'class' => 'span3',
                'value' => set_value('name', $support_object2)
            ),
            'support_date2' => array('name' => 'support_date2', 'id' => 'support_date2',
                'class' => 'span3',
                'value' => set_value('name', $support_date2)
            ),
            'support_result2' => array(
                'name' => 'support_result2',
                'id' => 'support_result2',
                'extra' => 'id="support_result2"',
                'options' => $option_result,
                'selected' => $support_result2
            ),
            'support_comment2' => array('name' => 'support_comment2', 'id' => 'support_comment2',
                'class' => 'span3',
                'value' => set_value('name', $support_comment2)
            ),
            // Tab 3-3
            'support_provider3' => array('name' => 'support_provider3', 'id' => 'support_provider3',
                'class' => 'span3',
                'value' => set_value('name', $support_provider3)
            ),
            'support_object3' => array('name' => 'support_object3', 'id' => 'support_object3',
                'class' => 'span3',
                'value' => set_value('name', $support_object3)
            ),
            'support_date3' => array('name' => 'support_date3', 'id' => 'support_date3',
                'class' => 'span3',
                'value' => set_value('name', $support_date3)
            ),
            'support_result3' => array(
                'name' => 'support_result3',
                'id' => 'support_result3',
                'extra' => 'id="support_result3"',
                'options' => $option_result,
                'selected' => $support_result3
            ),
            'support_comment3' => array('name' => 'support_comment3', 'id' => 'support_comment3',
                'class' => 'span3',
                'value' => set_value('name', $support_comment3)
            ),
            // Tab 3-4
            'support_provider4' => array('name' => 'support_provider4', 'id' => 'support_provider4',
                'class' => 'span3',
                'value' => set_value('name', $support_provider4)
            ),
            'support_object4' => array('name' => 'support_object4', 'id' => 'support_object4',
                'class' => 'span3',
                'value' => set_value('name', $support_object4)
            ),
            'support_date4' => array('name' => 'support_date4', 'id' => 'support_date4',
                'class' => 'span3',
                'value' => set_value('name', $support_date4)
            ),
            'support_result4' => array(
                'name' => 'support_result4',
                'id' => 'support_result4',
                'extra' => 'id="support_result4"',
                'options' => $option_result,
                'selected' => $support_result4
            ),
            'support_comment4' => array('name' => 'support_comment4', 'id' => 'support_comment4',
                'class' => 'span3',
                'value' => set_value('name', $support_comment4)
            ),
            // Tab 4
            'bilancoach_acquis' => array(
                'name' => 'bilancoach_acquis',
                'id' => 'bilancoach_acquis',
                'extra' => 'id="bilancoach_acquis"',
                'options' => $option_bilancoach_acquis,
                'selected' => $bilancoach_acquis
            ),
            'bilancoach_comment' => array('name' => 'bilancoach_comment', 'id' => 'bilancoach_comment',
                'class' => 'span3',
                'cols' => 6,
                'rows' => 3,
                'value' => set_value('name', $bilancoach_comment)
            ),
            'bilancoach_date' => array('name' => 'bilancoach_date', 'id' => 'bilancoach_date',
                'class' => 'span3',
                'value' => set_value('name', $bilancoach_date)
            ),
            'active' => array(
                'name' => 'active',
                'class' => 'class="checkActive"',
                'value' => 1,
                'checked' => (isset($active) && $active > 0) ? TRUE : FALSE
            ),
            'submit' => array('class' => 'btn btn-primary', 'data-loading-text' => $CI->lang->line('Loading'),
                'id' => 'form-submit', 'value' => $CI->lang->line('Save')),
        );

        return $_field_form;
    }

}

//define field to validate form
if (!function_exists('iaDignosisFieldValidate')) {

    function iaDignosisFieldValidate()
    {
        $CI = & get_instance();
        $_field_validate = array(
            array(
                'field' => 'member_id',
                'label' => $CI->lang->line('Question_Text'),
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'owner_id',
                'label' => $CI->lang->line('Order'),
                'rules' => 'trim|is_natural|xss_clean'
            ),
        );
        return $_field_validate;
    }

}